package com.wzc.chatclient;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;

public class ServerApplication {
    public static BufferedReader in1;
    public static BufferedReader in2;
    public static PrintWriter out1;
    public static PrintWriter out2;

    public static class ConnectionOne extends Thread{
        @Override
        public void run() {
            try {
                // 创建ServerSocket对象，指定监听的端口号
                ServerSocket serverSocket1 = new ServerSocket(54088);

                System.out.println("等待用户1连接...");

                // 监听客户端的连接请求
                Socket clientSocket1 = serverSocket1.accept();
                System.out.println("用户1已连接客户端");

                // 获取输入流和输出流 输入流和输出流是通过socket对象来进行数据传输的。
                in1 = new BufferedReader(new InputStreamReader(clientSocket1.getInputStream()));
                out1 = new PrintWriter(clientSocket1.getOutputStream(), true);

                String message1;

                while (true) {/*
                    */
                    // 读取客户端发送的信息
                    message1 = in1.readLine();

                    if (message1 == null || message1.equalsIgnoreCase("xzijbczxchihiuxchiuh呼吸组赤髭须和从自uhzihczihcziuzhcizxhhzixchweuqpgfsgygvzpgpzgv")) {
                        // 如果接收到终止标志，退出循环
                        break;
                    }

                    System.out.println("收到用户1消息：" + message1);

                    // 发送响应给客户端
                    out1.println("已收到你的消息：" + message1);
                    try {
                        out2.println("用户1消息：" + message1);
                    } catch (NullPointerException e) {
                        out1.println("用户2未连接!");
                    }

                }

                // 关闭连接
                out1.close();
                in1.close();
                clientSocket1.close();
                serverSocket1.close();
                //input1.close();
                interrupt();
            } catch (IOException e) {
                //noinspection CallToPrintStackTrace
                e.printStackTrace();
            }
        }
    }

    public static class ConnectionTwo extends Thread{
        @Override
        public void run() {
            try {
                // 创建ServerSocket对象，指定监听的端口号
                ServerSocket serverSocket2 = new ServerSocket(58840);

                System.out.println("等待用户2连接...");

                // 监听客户端的连接请求
                Socket clientSocket2 = serverSocket2.accept();
                System.out.println("用户2已连接客户端");

                // 获取输入流和输出流 输入流和输出流是通过socket对象来进行数据传输的。
                in2 = new BufferedReader(new InputStreamReader(clientSocket2.getInputStream()));
                out2 = new PrintWriter(clientSocket2.getOutputStream(), true);

                String message2;

                while (true) {

                    // 读取客户端发送的信息
                    message2 = in2.readLine();

                    if (message2 == null || message2.equalsIgnoreCase("xzijbczxchihiuxchiuh呼吸组赤髭须和从自uhzihczihcziuzhcizxhhzixchweuqpgfsgygvzpgpzgv")) {
                        // 如果接收到终止标志，退出循环
                        break;
                    }

                    System.out.println("收到用户2消息：" + message2);

                    // 发送响应给客户端
                    out2.println("已收到你的消息：" + message2);
                    try {
                        out1.println("用户2消息：" + message2);
                    } catch (NullPointerException e) {
                        out2.println("用户1未连接!");
                    }

                }

                // 关闭连接
                out2.close();
                in2.close();
                clientSocket2.close();
                serverSocket2.close();
                //input2.close();
                interrupt();
            } catch (IOException e) {
                //noinspection CallToPrintStackTrace
                e.printStackTrace();
            }
        }
    }
    public static void main(String[] args) {
        ConnectionOne connection_1 = new ConnectionOne();
        ConnectionTwo connection_2 = new ConnectionTwo();
        connection_1.start();
        connection_2.start();
        while (true){
            try {
                if (connection_1.isInterrupted()) {
                    connection_1 = new ConnectionOne();
                    connection_1.start();
                }
                if (connection_2.isInterrupted()) {
                    connection_2 = new ConnectionTwo();
                    connection_2.start();
                }
            }catch (Exception e){
                break;
            }
        }
        System.out.println("错误，请手动重启");
    }


}
